const app = getApp();
var util = require('../../utils/cache.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    login_member: '',         //输入的手机号
    login_code: null,         //传过来的验证码
    input_login_code: '',     //用户输入的验证码
    get_code_status: true,    //是否能点击获取验证码的状态判断
    show_get_code: '发送验证码',
    get_code_time: 60
  },


  input_val: function (e) {
    var userphone = e.detail.value;
    this.setData({
      login_member: userphone
    })
  },
  check: function () {
    var that = this;
    if (!this.data.get_code_status) {
      wx.showToast({
        title: '正在获取',
        icon: 'none',
        duration: 1000
      })
      return;
    } else {
      if (this.data.login_member.length == 11) {
        var myreg = /^1[34578]\d{9}$/;
        if (!myreg.test(this.data.login_member)) {
          wx.showToast({
            title: '请输入正确的手机号',
            icon: 'none',
            duration: 1000
          });
          return;
        } else {
          this.get_code();
        }
      } else {
        wx.showToast({
          title: '请输入完整手机号',
          icon: 'none',
          duration: 2000
        })
        return;
      }
    }
  },

  get_code: function () {
    var that = this;
    var timer = setInterval(function () {
      if (that.data.get_code_time > 0) {
        // console.log(that.data.get_code_time);
        that.setData({
          get_code_time: that.data.get_code_time - 1,
          show_get_code: '剩余' + (that.data.get_code_time - 1) + '秒',
          get_code_status: false
        });
      } else {
        clearInterval(timer);
        that.setData({
          get_code_time: 60,
          show_get_code: '获取验证码',
          get_code_status: true
        });
      }
    }, 1000);
 
    wx.request({
      url: app.https() + 'weixin/getVerifyCode',
      data: {
        phone: that.data.login_member
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      method: "POST",
      success: function (res) {
         console.log(res)
        if (res.data.resCode === 2001) {
          util.put('phonecode', res.data.resData.code, 60);  //存储code缓存
          that.setData({
            login_code: res.data.resData.code,      //后台返回的验证码，可以做判断用
          });
        } else {
          wx.showToast({
            title: '发送短信失败',
            icon: 'none',
            duration: 1000
          });
          clearInterval(timer);
          that.setData({
            get_code_time: 60,
            show_get_code: '获取验证码',
            get_code_status: true
          });
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '请求失败',
          icon: 'none',
          duration: 1000
        });
        clearInterval(timer);
        that.setData({
          get_code_time: 60,
          show_get_code: '获取验证码',
          get_code_status: true
        });
      }
    });
  },

  // 表单提交
  formSubmit: function (e) {
    var that = this;
    var formData = this.data.formData;
    var formValue = e.detail.value;
    var code = util.get('phonecode');
    var session_key = wx.getStorageSync('userkey_');
    var userphone = wx.getStorageSync('userphone_');
    console.log(formValue, code)
    // 收集表单数据
    if (formValue.phone === '') {
      wx.showToast({
        title: '手机号码不能为空',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (!(/^1[34578]\d{9}$/.test(formValue.phone))) {
      wx.showToast({
        title: '手机号码格式不正确',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (formValue.phone === userphone.phone) {
      wx.showToast({
        title: '手机号不能为当前手机号',
        icon: 'none',
        duration: 2000
      })
      return false;
    }

    if (formValue.code === '') {
      wx.showToast({
        title: '验证码不能为空',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (formValue.code != code) {
      wx.showToast({
        title: '验证码错误',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    //提交表单数据
    wx.showToast({
      title: '提交中',
      icon: 'loading',
      duration: 2000
    })
    console.log(formValue.phone, session_key.openid, formValue.code)
    wx.request({
      url: app.https() + 'weixin/editPhone',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: "POST",
      data: {
        phone: formValue.phone,
        openid: session_key.openid,
        code: formValue.code,
      },
      success: function (res) {
        console.log(res)
        if (res.data.resCode === 2001) {
          wx.showToast({
            // title: '提交成功',
            title: '修改成功！',
            icon: 'none',
            duration: 2000
          })
          var start_shop = res.data.resData.shops[0];
          wx.setStorageSync('start_shop_', start_shop);
          wx.setStorageSync('all_start_shop_', res.data.resData);
          wx.setStorageSync('userphone_', res.data.resData.phone); //存储用户信息
         
        } else if (res.data.resCode == 3001) {
          console.log(res)
          wx.setStorageSync('userphone_', res.data.resData.phone); //存储用户信息
          that.setData({
            phone: res.data.resData.phone,
            shops: [],
            activeId: '',
          });
          
        } else {
          console.log(res, '4001')
          wx.showToast({
            title: '修改失败！',
            icon: 'none',
            duration: 2000
          })
        }
        wx.switchTab({
          url: '../my/index',
        })
      }
    })
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})